package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author houChen
 * @date 2022/2/27 16:54
 * @Description: 创建延时队列和交换机 以及绑定
 */
@Configuration
public class MyMqConfig {


   /* @RabbitListener(queues = "order.release.order.queue")
    public void listenner(OrderEntity entity, Channel channel, Message message) throws IOException {
        System.out.println("收到过期的订单信息，准备关闭订单" + entity.getOrderSn());
        //接收到消息后返回ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }*/


    /**
     * 容器中的 Binding Queue Exchange会在RabbitMQ服务器中自动创建
     */
   /* @Bean
    public Queue orderDelayQueue(){
        //设置死信队列的属性
        //x-dead-letter-exchange: order-event-exchange
        //x-dead-letter-routing-key: order.release.order
        //x-message-ttl: 6000
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        arguments.put("x-message-ttl",60000);
        //public Queue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
        Queue queue = new Queue("order.delay.queue", true, false, false,arguments);
        return queue;
    }

    @Bean
    public Queue orderReleaseOrderQueue(){
        return new Queue("order.release.order.queue", true, false, false);
    }

    @Bean
    public Exchange orderEventExchange(){
        //public TopicExchange(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments)
        return new TopicExchange("order-event-exchange",true,false);
    }

    @Bean
    public Binding orderCreateOrderBinding(){
        //public Binding(String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments)
        Binding binding = new Binding("order.delay.queue",Binding.DestinationType.QUEUE,
                "order-event-exchange","order.create.order",null);
        return binding;

    }

    @Bean
    public Binding orderReleaseOrderBinding(){
        Binding binding = new Binding("order.release.order.queue",Binding.DestinationType.QUEUE,
                "order-event-exchange","order.release.order",null);
        return binding;
    }*/


    /**
     * @Description:  订单释放直接和解锁库存进行绑定
     * @param null
     * @author  houChen
     * @date  2022/3/6 10:28
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        Binding binding = new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange", "order.release.other.#", null);
        return binding;
    }
}